package com.xpgk.xpsoft.login;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.jfinal.aop.Clear;
import com.jfinal.core.ActionKey;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.common.controller.BaseController;
import com.xpgk.common.kit.MailKit;
import com.xpgk.common.kit.NetKit;
import com.xpgk.common.kit.PasswordKit;
import com.xpgk.model.FrmUser;
import com.xpgk.xpsoft.system.SystemService;
import com.xpgk.xpsoft.user.UserService;

@Clear
public class LoginController extends BaseController {
	
	public void index() {
		render("login.html");
	}

	public void getAuthCode() {
		renderCaptcha();
	}

	public void login() {
		// 获取表单信息
		String userName = getPara("userName");
		String password = getPara("password");
		String returnJson = getPara("returnText");

		// 如果是httpclient登陆就不处理验证码，不用担心密码暴力破解，因为init文件有密码错误次数限制
		if (StrKit.notBlank(returnJson)) {
			String result = LoginService.login(userName, password, NetKit.getClientIP(getRequest()));
			if (result.equals("ok"))
				SetCurUser(userName, UserService.getUser(userName).getInt("id"));
			renderText(result);
			return;
		} else {
			boolean authCode = validateCaptcha("authCode"); // 验证验证码
			if (PropKit.getBoolean("devMode"))
				authCode = true;
			if (authCode) {
				String result = LoginService.login(userName, password, NetKit.getClientIP(getRequest()));
				if (result.equals("ok")) { // 登陆验证成功
					int userID = UserService.getUser(userName).getInt("id");
					SetCurUser(userName, userID);
					if (HashKit.md5("888888").equals(password)) {
						setAttr("userID", userID);
						renderMsg("upPWD", "login.html");
						return;
					}
					redirect("/xpsoft");
					return;
				}
				keepPara();
				renderMsg(result, "login.html");
				return;
			} else {
				keepPara();
				renderMsg("错误的验证码。", "login.html");
				return;
			}
		}
	}

	@ActionKey("/xpLogout")
	public void logout() {
		LoginService.logout(getCurUserName(), NetKit.getClientIP(getRequest()));
		SetCurUser(null, -1);
		redirect("/xpLogin");
	}

	// 找回密码
	public void findPassword_ui() {
		render("findpassword.html");
	}

	// 找回密码
	public void findPassword() {
		keepPara();
		// String host = "smtp.163.com"; // 发送邮件的服务器的IP
		// String port = "25"; // 发送邮件的服务器的端口
		//
		// String from = "dongcb678@163.com"; // 邮件发送者的地址
		// String userName = "dongcb678@163.com"; // 登陆邮件发送服务器的用户名
		// String password = "xxx"; // 登陆邮件发送服务器的密码
		//
		// List<String> to = new ArrayList<String>(); // 邮件接收者的地址
		// to.add("150584428@qq.com");
		//
		// boolean validate = true; // 是否需要身份验证
		//
		// String subject = "标题test111"; // 邮件标题
		// String content = "内容test111"; // 邮件的文本内容
		// String[] attachFileNames = new String[]{"D:/code.jpg"}; // 邮件附件的文件名
		String userName = getPara("userName");
		FrmUser user = UserService.getUser(userName);
		if (user != null) {
			if (user.getNAME().equals(getPara("name")) && user.getEMAIL().equals(getPara("email"))) {
				Random random = new Random(new Date().getTime());
				String password = String.valueOf(Math.abs(random.nextLong() % 1000000));
				UserService.setPassword(userName, HashKit.md5(password));

				String host = PropKit.get("email_host"); // 发送邮件的服务器的IP
				String port = PropKit.get("email_port"); // 发送邮件的服务器的端口

				String from = PropKit.get("email_from"); // 邮件发送者的地址
				String emailuserName = PropKit.get("email_userName"); // 登陆邮件发送服务器的用户名
				String emailpassword = PasswordKit.getPassword("email_password"); // 登陆邮件发送服务器的密码

				List<String> to = new ArrayList<String>(); // 邮件接收者的地址
				to.add(getPara("email"));

				boolean validate = true; // 是否需要身份验证

				Record system = SystemService.GetSystem();

				String subject = system.getStr("name") + ",找回密码"; // 邮件标题
				String content = "尊敬的用户：" + getPara("userName") + "，你好，你的登录密码为\"" + password + "\""; // 邮件的文本内容
				String[] attachFileNames = null;// new String[]{"D:/code.jpg"};
												// // 邮件附件的文件名

				MailKit.sendTextMail(host, port, validate, emailuserName, emailpassword, from, to, subject, content,
						attachFileNames);
			}
		}
		renderMsg("如果你输入的信息匹配，重置的密码将会发到你的邮箱，请注意查收！", "findpassword.html", true);
	}
}
